var ctx;

function setCtx(ctx) {
	this.ctx = ctx;
}

//验证优惠券名称
function checkName(){
	var name = $("#name").val();
	//解决ie9以下placeholder与value重复的问题
	if(name == $("#name").attr("placeholder")){
		name = "";
	}
	if(name == null || name == ""){
		layer.tips('名称不能为空!', '#name', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		return true;
	}
}

//验证优惠券类型
function checkType(){
	var type = $("#type").val();
	if(type == null || type == ""){
		layer.tips('请选择优惠券类型!', '#type', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		return true;
	}
}

//验证领取有效期的开始时间
function checkBeginDate(){
	var beginDate = $("#beginDate").val();
	//解决ie9以下placeholder与value重复的问题
	if(beginDate == $("#beginDate").attr("placeholder")){
		beginDate = "";
	}
	if(beginDate == null || beginDate == ""){
		layer.tips('请选择开始时间!', '#beginDate', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		return true;
	}
}

//验证领取有效期的结束时间
function checkEndDate(){
	var endDate = $("#endDate").val();
	//解决ie9以下placeholder与value重复的问题
	if(endDate == $("#endDate").attr("placeholder")){
		endDate = "";
	}
	if(endDate == null || endDate == ""){
		layer.tips('请选择结束时间!', '#endDate', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
    	return true;
    }
}

//验证使用有效期的开始时间
function checkUseBeginDate(){
	var beginDate = $("#beginDate").val();
	var useBeginDate = $("#useBeginDate").val();
	//解决ie9以下placeholder与value重复的问题
	if(beginDate == $("#beginDate").attr("placeholder")){
		beginDate = "";
	}
	if(useBeginDate == $("#useBeginDate").attr("placeholder")){
		useBeginDate = "";
	}
	var begin = new Date(beginDate.replace("-", "/").replace("-", "/"));  
    var start = new Date(useBeginDate.replace("-", "/").replace("-", "/"));  
	if(useBeginDate == null || useBeginDate == ""){
		layer.tips('请选择开始时间!', '#useBeginDate', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else if(start < begin){
    	layer.msg("使用有效期的起始时间应大于领取有效期的起始时间",{
			icon: 3,
			time: 4000 //2秒关闭（如果不配置，默认是3秒）
		});
    	return false;
    }else{
    	return true;
    }
}

//验证使用有效期的结束时间
function checkUseEndDate(){
	var endDate = $("#endDate").val();
	var useEndDate = $("#useEndDate").val();
	//解决ie9以下placeholder与value重复的问题
	if(endDate == $("#endDate").attr("placeholder")){
		endDate = "";
	}
	if(useEndDate == $("#useEndDate").attr("placeholder")){
		useEndDate = "";
	}
	var end = new Date(endDate.replace("-", "/").replace("-", "/"));  
    var stop = new Date(useEndDate.replace("-", "/").replace("-", "/"));  
	if(endDate == null || endDate == ""){
		layer.tips('请选择结束时间!', '#endDate', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else if(stop < end){
    	layer.msg("使用有效期的结束时间应大于领取有效期的结束时间",{
			icon: 3,
			time: 4000 //2秒关闭（如果不配置，默认是3秒）
		});
    	return false;
    }else{
    	return true;
    }
}

//验证减免线
function checkBreakLine(){
	var breakLine = $("#breakLine").val();
	if(breakLine == null || breakLine == ""){
		layer.tips('减免线不能为空!', '#breakLine', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else if(parseInt(breakLine) < 0){
		layer.tips('请输入正整数!', '#breakLine', {
			tips: 3,
			time:1000
		});
		return false;
	}else{
		return true;
	}
}

//验证减免金额
function checkBreakPrice(){
	var breakPrice = $("#breakPrice").val();
	if(breakPrice == null || breakPrice == ""){
		layer.tips('减免金额不能为空!', '#breakPrice', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else if(parseInt(breakPrice) < 0){
		layer.tips('请输入正数!', '#breakPrice', {
			tips: 3,
			time:1000
		});
		return false;
	}else{
		return true;
	}
}

//验证数量
function checkCount(){
	var reg = /^[0-9]*[1-9][0-9]*$/;
	var count = $("#count").val();
	if(count == null || count == ""){
		layer.tips('优惠券数量不能为空!', '#count', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else if(!reg.test(count)){
		layer.tips('请输入正整数!', '#count', {
			tips: 3,
			time:1000
		});
		return false;
	}else{
		return true;
	}
}

//保存或新增方法
function saveCouponInfo() {
	var couponInfoId = $("#couponInfoId").val();
	var name = $("#name").val();
	var type = $("#type").val();
	var breakLine = $("#breakLine").val();
	var breakPrice = $("#breakPrice").val();
	var beginDate = $("#beginDate").val();
	var endDate = $("#endDate").val();
	var useBeginDate = $("#useBeginDate").val();
	var useEndDate = $("#useEndDate").val();
	var count = $("#count").val();
	
    if(checkName() && checkType() && checkBeginDate() && checkEndDate() && checkUseBeginDate() 
    		&& checkUseEndDate() && checkBreakLine() && checkBreakPrice() && checkCount()){
	    	var index1 = layer.load(1);
				$.ajax({
					type : "POST",
					url : ctx + "/couponInfo/saveOrUpdateCouponInfo",
					cache : false,
					dataType : 'json',
					data: {
						id:couponInfoId,
						name:name,
						type:type,
						breakLine:breakLine,
						breakPrice:breakPrice,
						beginDate:beginDate,
						endDate:endDate,
						count:count,
						useBeginDate:useBeginDate,
						useEndDate:useEndDate
					},
					success : function(result) {
						if (result.success) {
							layer.msg("操作成功",{
								icon: 1,
								time: 2000 //2秒关闭（如果不配置，默认是3秒）
							},function(){
								loadContent("/couponInfo/couponInfoList");
							});
						} else {
							layer.close(index1);
							layer.msg("操作失败",{
								icon: 2
							});
						}
					}
				});
    }
}

//返回上一页
function cancel(){
	loadContent("/couponInfo/couponInfoList"); 
}